import "dart:developer";

import "package:sports_new/entity/home/task/task_entity.dart";
import "package:sports_new/entity/king/king_scroll_entity.dart";
import "package:sports_new/entity/other/play_entity.dart";
import "package:sports_new/utils/user.dart";

import "../../entity/king/king_entity.dart";
import "../../entity/king/king_history_entity.dart";
import "../../entity/king/king_list_entity.dart";
import "../../entity/king/king_rank_entity.dart";
import "../api_dir.dart";
import "../http/dio_utils.dart";

class KingApi {
  static Future<KingListEntity> getKingList(String date) async {
    try {
      final list = await DioUtils.post(
          "/${ApiDir.activity}/app-ag-match/match/list", params: {"id": date});
      KingListEntity entity = KingListEntity.fromJson(list.data["d"]);
      if(User.info?.id != null) {
        final count = await DioUtils.post("/${ApiDir.activity}/app-ag-match/guess/count");
        entity.sendCount = KingEntity.fromJson(count.data["d"]).sendCount;
        entity.saveCount = KingEntity.fromJson(count.data["d"]).saveCount;
      }
      return entity;
    } catch (err) {
      log(err.toString());
      return KingListEntity();
    }
  }

  static Future<int?> sendPrediction(String? item, int? matchId) async {
    final params = {"item": item, "matchId": matchId};
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-ag-match/guess/match", params: params);
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  static Future<KingEntity> getKingDetail(int matchId) async{
    final params = {"id": matchId};
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-ag-match/match/info", params: params);
    try {
      KingEntity entity = KingEntity.fromJson(result.data["d"]);
      return entity;
    } catch(e){
      return KingEntity();
    }
  }

  static Future<KingRankEntity> getKingRank(
    String time,int rankType,int page,int pageSize) async{
    final params = {
      "data": {"time": time, "type": rankType},
      "page": page, "pageSize": pageSize
    };
    try {
      final result =
      await DioUtils.post("/${ApiDir.activity}/app-ag-match/rank/list", params: params);
      KingRankEntity entity = KingRankEntity.fromJson(result.data["d"]);
      return entity;
    } catch(e){
      log(e.toString());
      return KingRankEntity();
    }
  }

  static Future<List<KingHistoryEntity>> getKingHistory(int page,int pageSize) async{
    final params = {
      "data": {},
      "page": page, "pageSize": pageSize
    };
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-ag-match/guess/list", params: params);
    try {
      List<KingHistoryEntity>? list = result.data["d"]["rows"]
          .map<KingHistoryEntity>((e) => KingHistoryEntity.fromJson(e)).toList();
      return list ?? [];
    } catch(e){
      log(e.toString());
      return [];
    }
  }

  //任务列表
  static Future<TaskEntity> getTask() async{
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-point/info");
    try {
      TaskEntity? entity = TaskEntity.fromJson(result.data["d"]);
      return entity;
    } catch(e){
      log(e.toString());
      return TaskEntity();
    }
  }

  //奖品列表
  static Future<List<MarketItem>> getMarket(int page,int pageSize) async{
    final params = {
      "data": {},
      "page": page, "pageSize": pageSize
    };
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-point/goods/list", params: params);
    try {
      List<MarketItem>? list = result.data["d"]["rows"]
          .map<MarketItem>((e) => MarketItem.fromJson(e)).toList();
      return list ?? [];
    } catch(e){
      log(e.toString());
      return [];
    }
  }

  //积分明细
  static Future<List<PointsHistory>> getPointsHistory(int page,int pageSize) async{
    final params = {
      "data": {},
      "page": page, "pageSize": pageSize
    };
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-point/detail/list", params: params);
    try {
      List<PointsHistory>? list = result.data["d"]["rows"]
          .map<PointsHistory>((e) => PointsHistory.fromJson(e)).toList();
      return list ?? [];
    } catch(e){
      log(e.toString());
      return [];
    }
  }

  //兑换奖品
  static Future<int?> exchange(int? id) async {
    final params = {"id": id};
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-point/goods/exchange", params: params);
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //领取积分
  static Future<int?> receive(int? id) async {
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-point/receive", params: id);
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //滚动条
  static Future<List<KingScrollEntity>> getScrollInfo() async {
    final result =
    await DioUtils.post("/${ApiDir.activity}/app-ag-match/scrollbar");
    try {
      List<KingScrollEntity>? list = result.data["d"]
          .map<KingScrollEntity>((e) => KingScrollEntity.fromJson(e)).toList();
      return list ?? [];
    } catch(e){
      log(e.toString());
      return [];
    }
  }
}